package com.intellagric.agriecom.service.agriecom_quotation;

import com.github.pagehelper.PageHelper;
import com.intellagric.agriecom.module.agriecom_quotation.QuotationService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.agriecom.agriecom_quotation.AgriecomQuotationDao;
import com.intellagric.mapper.AgriecomQuotationMapper;
import com.intellagric.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
    public class QuotationServiceImpl implements QuotationService {

    @Autowired
    private AgriecomQuotationMapper quotationMapper;
    @Autowired
    private AgriecomQuotationDao quotationDao;

    @Override
    public ResponseMessage addQuotation(AgriecomQuotation quotattion) {
        if(quotationMapper.insertSelective(quotattion)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();

    }

    @Override
    public ResponseMessage deleteQuotation(String id) {
        if(quotationMapper.deleteByPrimaryKey(id)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editQuotation(AgriecomQuotation quotation) {
        if(quotationMapper.updateByPrimaryKeySelective(quotation)==1)
            return ResponseMessage.success();
        else
            return ResponseMessage.fail();
    }

    @Override
    public AgriecomQuotation getQuotationById(String id) {
        return quotationMapper.selectByPrimaryKey(id);
    }


    @Override
    public LayuiDataGridResult getQuotationList() {
        AgriecomQuotationExample quotationExample=new AgriecomQuotationExample();
        quotationExample.createCriteria().andIdIsNotNull();
        quotationExample.setOrderByClause("record_time desc");
        List<AgriecomQuotation> agriecomQuotationList = quotationMapper.selectByExample(quotationExample);


        return LayuiDataGridResult.success().add(agriecomQuotationList,agriecomQuotationList.size());
    }
    /**
     * 按时间排序行情并分页
     * @return
     */
    @Override
    public LayuiDataGridResult getQuotationList(int page, int rows) {
        PageHelper.startPage(page,rows);


        return getQuotationList();
    }

    @Override
    public ResponseMessage batchDeleteQuotation(String[] ids) {
        if (quotationDao.batchDeleteByPrimaryKey(ids)<1)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }
    //根据商品id修改行情
    @Override
    public ResponseMessage priceChange(String id,float price) throws ParseException {
        AgriecomQuotationExample example=new AgriecomQuotationExample();
        example.createCriteria().andProduceIdEqualTo(id);
        List<AgriecomQuotation> quotations = quotationMapper.selectByExample(example);
        if(quotations!=null&&quotations.size()!=0){
            for(AgriecomQuotation quotation:quotations){
                String trend=quotation.getTrend();
                String [] prices=trend.split(",");
                StringBuilder str=new StringBuilder();
                for(int i=0;i<6;i++)
                {
                    str.append(prices[i+1]);
                    str.append(",");
                }
                str.append(price);
                quotation.setTrend(str.toString());
                //更新记录时间、转换抛出异常
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                quotation.setRecordTime(format.parse(format.format(new Date(System.currentTimeMillis()))));
                quotationMapper.updateByPrimaryKey(quotation);
            }
            return ResponseMessage.success("success");
        }
        return ResponseMessage.fail("failure");
    }

    //根据商品id联动删除行情信息
    @Override
    public LayuiDataGridResult deleteByProIds(String[] ids) {
        int count=quotationDao.batchDeleteByProIds(ids);
        if(count!=0)
            return LayuiDataGridResult.success();
        else{
            LayuiDataGridResult layuiDataGridResult=new LayuiDataGridResult();
            layuiDataGridResult.setMsg("查无此商品行情或删除失败");
            return layuiDataGridResult;
        }

    }

    @Override
    public LayuiDataGridResult getquotationByKeyword(int page, int limit, String keyword) {
        PageHelper.startPage(page, limit);

        AgriecomQuotationExample quotationExample=new AgriecomQuotationExample();

        quotationExample.createCriteria().andNameLike("%"+keyword+"%");

        List<AgriecomQuotation> quotationList=quotationMapper.selectByExample(quotationExample);

        return LayuiDataGridResult.success().add(quotationList,quotationList.size() );
    }

    @Override
    public LayuiDataGridResult getQuotationByCategory(List<AgriecomProduce> list, int page, int limit){
        PageHelper.startPage(page,limit);
        List<String> ids=new ArrayList<>();
        for(AgriecomProduce produce:list){
            ids.add(produce.getProduceId());
        }
        //
        if(ids.size()==0){
            LayuiDataGridResult layuiDataGridResult=new LayuiDataGridResult();
            layuiDataGridResult.setMsg("暂无数据");
            return layuiDataGridResult;
        }
        List<AgriecomQuotation> quotations=quotationDao.selectByProduceIds(ids);
        LayuiDataGridResult lay=new LayuiDataGridResult();
        lay.add(quotations,quotations.size());
        lay.setMsg("success");
        return lay;
    }

}
